﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;
using System.Web;
using System.IO;

namespace IHISCETeim.BLL
{
    public class JSDWFFolderController
    {
        #region 文件夹目录配置
        /// <summary>
        /// 当前应用程序根目录
        /// </summary>
        public static string ApplicationPath
        {
            get
            {

                return HttpContext.Current.Request.PhysicalApplicationPath;
            }
        }
        /// <summary>
        /// DWF目录所在,相对路径
        /// </summary>
        public static string DWFUploadFolderPath
        {
            get
            {
                return "/upload/jsdwf/";
            }
        }
        #endregion
        /// <summary>
        /// 返回-1表示目录已存在，请重新输入目录名称
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(JSDWFFolderInfo obj)
        {
            
            JSDWFFolderInfo parFolderobj = GetByFolderID(obj.ParFolderID);

            string FolderPath = (parFolderobj == null ? DWFUploadFolderPath : (parFolderobj.FolderPath + "/")) + obj.FolderName;
            string path = ApplicationPath + FolderPath;
            //判断目录是否存在,存在的话则返回错误提示-1
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            else
                return -1;

            obj.FolderPath = FolderPath;

            return JSDWFFolderDAL.Add(obj);
        }
        /// <summary>
        /// 删除目录,目录不为空则无法删除代码-1
        /// </summary>
        /// <param name="FolderID"></param>
        /// <returns></returns>
        public static int Delete(int FolderID)
        {
            JSDWFFolderInfo obj = GetByFolderID(FolderID);
            string path =ApplicationPath+ obj.FolderPath;
            try
            {
                if (Directory.Exists(path))
                {
                    Directory.Delete(path);
                }
            }
            catch
            {
                return -1;
            }
            return JSDWFFolderDAL.Delete(FolderID)?1:0;
        }

        public static List<JSDWFFolderInfo> GetAllByParFolderId(int ParFolderID)
        {
            return CBO.FillCollection<JSDWFFolderInfo>( JSDWFFolderDAL.GetAllByParFolderId(ParFolderID));
        }

        public static JSDWFFolderInfo GetByFolderID(int FolderID)
        {
            return CBO.FillObject<JSDWFFolderInfo>(JSDWFFolderDAL.GetByFolderID(FolderID));

        }
        public static DataTable GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return JSDWFFolderDAL.GetPagination(WhereSql, PageIndex, PageSize, Sort, ref TotalCount);
        }
        /// <summary>
        /// 返回－1，表示所属上级的目录下已存在相应的文件，无法再次创建，请确认
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Update(JSDWFFolderInfo obj)
        {
            //新的信息
            JSDWFFolderInfo parFolderobj = GetByFolderID(obj.ParFolderID);
            string FolderPath = (parFolderobj == null ? DWFUploadFolderPath : (parFolderobj.FolderPath+ "/") )+ obj.FolderName;
            obj.FolderPath = FolderPath;
          //  string path = ApplicationPath + FolderPath;
            //原来的信息
            JSDWFFolderInfo oldFolderObj = GetByFolderID(obj.FolderID);
            //1。如果更换上级目录
            if (oldFolderObj.ParFolderID != obj.ParFolderID)
            { 
                //
                if (!Directory.Exists(ApplicationPath + obj.FolderPath))
                {
                    Directory.Move(ApplicationPath + oldFolderObj.FolderPath, ApplicationPath + obj.FolderPath);
                }
                else
                {
                    return -1;
                }
            }
            //2.如果是更换了名称
            if (oldFolderObj.FolderName != obj.FolderName)
            {
                Directory.Move(ApplicationPath + oldFolderObj.FolderPath, ApplicationPath + obj.FolderPath);
            }

            return JSDWFFolderDAL.Update(obj)?1:0;
        }
    }
}
